package com.hgk.wanwan.main.fragment.persentfragment.alarmclock;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.RemoteViews;

import width.hgk.wanwan.R;

public class AppWidget extends AppWidgetProvider {

	private DatabaseHelper dbHelper;
	String[] desk_text;
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		//��ȡ��ݿ��еļ�¼
		dbHelper = new DatabaseHelper(context, "ideal.sql");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query("user", null, null, null, null, null, null);
		int desk_num=0;
		//����ı�ǩֻ��ʾ6����¼
		desk_text = new String[6];
		while(cursor.moveToNext()){
			System.out.println("====cursor.movetonext"); 
			if(desk_num == 6){
				break;
			}
			String temp_text = cursor.getString(cursor.getColumnIndex("mtext"));
			//����ÿ����¼��ʾ�ĳ���
			if(temp_text.length()>7){
				temp_text=temp_text.substring(0, 7)+"...";
			}
			desk_text[desk_num]=temp_text;
			System.out.println("desk_text[desk_num]"+desk_text[desk_num]);
			desk_num++;			
		}
		db.close();
		final int Num = appWidgetIds.length;
		for (int i = 0; i < Num; i++) {
			int[] mAppWidgetId = appWidgetIds;
			RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget);
			mRemoteViews.setTextViewText(R.id.desktop_text, array_to_string(desk_text));

			Intent clickIntent = new Intent(context, AlarmActivity.class);
			PendingIntent pdIntent = PendingIntent.getActivity(context, 0,
					clickIntent, 0);
			mRemoteViews.setOnClickPendingIntent(R.id.widget_layout, pdIntent);
			appWidgetManager.updateAppWidget(mAppWidgetId, mRemoteViews); 
		}
	}
	//onReceive�����������չ㲥���Ա��������ı�ǩ
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		if(intent.getAction().equals("com.ideal.note.widget")){
			dbHelper = new DatabaseHelper(context, "ideal.sql");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query("user", null, null, null, null, null, null);
		int desk_num=0;
		desk_text = new String[6];
		System.out.println("onreceive####");
		while(cursor.moveToNext()){
			if(desk_num == 6){
				break;
			}
			String temp_text = cursor.getString(cursor.getColumnIndex("mtext"));
			if(temp_text.length()>7){
				temp_text=temp_text.substring(0, 7)+"...";
			}
			desk_text[desk_num]=temp_text;
			System.out.println("desk_text[desk_num]"+desk_text[desk_num]);
			desk_num++;			
		}
		db.close();
		RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
		mRemoteViews.setTextViewText(R.id.desktop_text, array_to_string(desk_text));
		AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, AppWidget.class), mRemoteViews);
		}
				
	}
	
	//����ת��Ϊ�ַ���ʾ
	public String array_to_string(String[] array){
		String temp_str = "";
		for(int i=0;i<array.length;i++){
			if(array[i]==null){
				break;
			}else {
				temp_str = temp_str+"\n* "+array[i];
			}
		}
		System.out.println("--->"+temp_str);
		return  temp_str;
	}
}
